package cn.bx.consumer.ctrl;

import org.example.pojo.Answer;
import org.example.pojo.Question;
import org.example.service.MainServiceI;
import org.example.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {
    @Autowired
    private MainServiceI mainServiceI;


    @RequestMapping(value = "searchAll", produces = "application/json;charset=utf-8")
    public AjaxResult searchAll() {
        return mainServiceI.searchAll();
    }

    @RequestMapping(value = "getBy", produces = "application/json;charset=utf-8")
    public AjaxResult getBy(@RequestParam(name = "id") long id){
        return mainServiceI.getBy(id);
    }

    @RequestMapping(value = "addQuestion", produces = "application/json;charset=utf-8")
    public AjaxResult addQuestion(@RequestParam(name = "title") String title, @RequestParam(name = "detaildesc") String detaildesc){
        Question question = new Question();
        question.setTitle(title);
        question.setDetaildesc(detaildesc);
        return mainServiceI.addQuestion(question);
    }

    @RequestMapping(value = "addAnswer", produces = "application/json;charset=utf-8")
    public AjaxResult addAnswer(@RequestParam(name = "qid") long qid, @RequestParam(name = "anscontent") String anscontent){
        Answer answer = new Answer();
        answer.setQid(qid);
        answer.setAnscontent(anscontent);
        return mainServiceI.addAnswer(answer);
    }

    @RequestMapping(value = "getAnswer", produces = "application/json;charset=utf-8")
    public AjaxResult getAnswer(@RequestParam(name = "qid") long id){
        return mainServiceI.getAnswer(id);
    }
}
